package com.ruoyi.web.controller.guarantee;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.guarantee.domain.BAgreement;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.guarantee.domain.BOrder;
import com.ruoyi.guarantee.service.IBOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 询单Controller
 *
 * @author ruoyi
 * @date 2025-01-17
 */
@RestController
@RequestMapping("/guarantee/order")
public class BOrderController extends BaseController
{
    @Autowired
    private IBOrderService bOrderService;

    /**
     * 查询询单列表
     */
    @PreAuthorize("@ss.hasPermi('guarantee:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(BOrder bOrder)
    {
        startPage();
        List<BOrder> list = bOrderService.selectBOrderList(bOrder);
        return getDataTable(list);
    }

    /**
     * 查询询单列表
     */
    @PreAuthorize("@ss.hasPermi('guarantee:order:checkList')")
    @GetMapping("/listCheck")
    public TableDataInfo listCheck(BOrder bOrder)
    {
        startPage();
        List<BOrder> list = bOrderService.selectBOrderCheckList(bOrder);
        return getDataTable(list);
    }

    /**
     * 导出询单列表
     */
    @PreAuthorize("@ss.hasPermi('guarantee:order:export')")
    @Log(title = "询单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BOrder bOrder)
    {
        List<BOrder> list = bOrderService.selectBOrderList(bOrder);
        ExcelUtil<BOrder> util = new ExcelUtil<BOrder>(BOrder.class);
        util.exportExcel(response, list, "询单数据");
    }

    /**
     * 获取询单详细信息
     */
    @PreAuthorize("@ss.hasPermi('guarantee:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bOrderService.selectBOrderById(id));
    }

    /**
     * 新增询单
     */
    @PreAuthorize("@ss.hasPermi('guarantee:order:add')")
    @Log(title = "询单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BOrder bOrder)
    {
        bOrder.setUserId(getUserId());
        bOrder.setCreateBy(getUsername());
        return toAjax(bOrderService.insertBOrder(bOrder));
    }

    /**
     * 修改询单
     */
    @PreAuthorize("@ss.hasPermi('guarantee:order:edit')")
    @Log(title = "询单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BOrder bOrder)
    {
        return toAjax(bOrderService.updateBOrder(bOrder));
    }

    /**
     * 删除询单
     */
    @PreAuthorize("@ss.hasPermi('guarantee:order:remove')")
    @Log(title = "询单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bOrderService.deleteBOrderByIds(ids));
    }

    /**
     * 删除询单
     */
    @PostMapping("/zhuanOrder/{id}")
    public AjaxResult zhuanOrder(@PathVariable Long id)
    {

        return toAjax(bOrderService.zhuanBOrderById(id));
    }

    @PostMapping("/tijiao")
    public AjaxResult tijiao( @RequestBody BOrder bOrder)
    {
        BOrder order = bOrderService.selectBOrderById(bOrder.getId());
        if (order == null) {
            return AjaxResult.error("该订单不存在");
        }
        order.setStatus(2L);//提交
        return AjaxResult.success(bOrderService.updateBOrder(order));
    }

    /**
     * 询单审核保存
     */
    @PostMapping("/check")
    public AjaxResult check(@RequestBody BOrder bOrder)
    {
        bOrder.setCheckUserId(getUserId());
        bOrder.setCheckTime(DateUtils.getNowDate());
        return toAjax(bOrderService.updateBOrder(bOrder));
    }

    /**
     * 一键复制
     */
    @GetMapping("/copySave/{id}")
    public AjaxResult copySave(@PathVariable Long id){
        BOrder bOrder = bOrderService.selectBOrderById(id);
        if (bOrder == null) {
            return AjaxResult.error("该订单不存在");
        }
        Map<String,String> maps = new HashMap<>();
        maps.put("受益人",bOrder.getSyName());
        maps.put("受益人企业性质",bOrder.getSyQyxz());
        maps.put("被保证人姓名",bOrder.getBbzrName());
        maps.put("被保证人企业性质",bOrder.getBbzrQyxz());
        maps.put("被保证人注册资金",bOrder.getBbzrZczj());
        maps.put("被保证人成立时间",bOrder.getBbzrDate());
        maps.put("对应资质",bOrder.getDyzz());
        maps.put("最高资质",bOrder.getZgzz());
        maps.put("保函类型",bOrder.getBhType());
        maps.put("保函格式",bOrder.getBhGs());
        maps.put("保函金额",bOrder.getBhMoney().toString());
        maps.put("项目名称",bOrder.getXmName());
        maps.put("担保期限",bOrder.getDbQixian());
        maps.put("中标时间或合同签署时间",DateUtils.dateTime(bOrder.getXmDate()));
        maps.put("工期",bOrder.getXmGongqi());
        maps.put("出函银行",bOrder.getXmChBank());
        maps.put("其他备注",bOrder.getRemark());
        return AjaxResult.success(maps);
    }
}
